package com.gorkr.labuladong.data_struct.merge_sort;

import org.junit.jupiter.api.Test;

/**
 * @author gorkr
 * @date 2022/06/05 11:33
 **/



public class E21MergeTwoSortedLists {

    class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode p = list1, q = list2;
        ListNode res=new ListNode(-999);
        ListNode head = res;
        while (p!=null && q!=null){
            if(p.val<q.val){
                head.next = p;
                head = head.next;
                p = p.next;

            }else {
                head.next = q;
                head = head.next;
                q = q.next;
            }
        }
        if(p==null){
            head.next = q;
        }else {
            head.next = p;
        }

        return res.next;
    }

    @Test
    public void test(){

    }
}
    